﻿'use strict';

QuotingToolApp
    .controller('SearchMyQuotesController', ['$scope', '$location', '$routeParams', 'myQuoteData', 'notifications', 'CommonServices', function ($scope, $location, $routeParams, myQuoteData, notifications, CommonServices) {
        var self = this;
        var selectedAccount;
        var selectedCustomer;     

        //********************** Search Quotes functions ***********************************************************
                
        self.init = function () {                        
            //initialize collections
            if (myQuoteData.quoteDatasCollection)
                self.quoteDatasCollection = myQuoteData.quoteDatasCollection;
            else
                self.quoteDatasCollection = [];

            self.autoCompleteAccountsColl = [];
            self.autoCompleteCustomersColl = [];                                       

            //visual styling
            angular.element('.pull-down').each(function () {
                var $this = $(this);
                $this.css('margin-top', $this.parent().height() - $this.height()- 100)
            });
            //Get User's recent quotes
            self.getMyRecentQuotes();
        };                                                  
       
        self.getMyRecentQuotes = function () {
            myQuoteData.getRecentQuotes("devuser")
                       .$promise
                       .then(function (results) {
                           if (results != null) {
                               self.addMyQuotesSearchResults(results);
                           }
                           else {
                               notifications.showWarning('Recent quotes not found');
                           }
                       })
                       .catch(function (response) {
                           notifications.showError('Error searching recent quotes');                           
                           console.log(response);
                       }
                   );        
        };

        self.searchQuotes = function (searchquotesform) {
            if (searchquotesform.$valid) {
                var searchParams = {
                    Quotenumber: (self.quoteNumberTxt == undefined || self.quoteNumberTxt == "") ? 'null' : self.quoteNumberTxt,
                    Fromdate: (self.dtPicker == undefined || self.dtPicker == "") ? 'null' : self.formatDate(self.dtPicker),
                    Todate: (self.dtPicker == undefined || self.dtPicker == "") ? 'null' : self.formatDate(self.dtPicker),
                    Createdby: (self.selectedEmployeeCreate == undefined || self.selectedEmployeeCreate == "") ? 'null' : self.selectedEmployeeCreate.UserName,
                    Updatedby: (self.selectedEmployeeModify == undefined || self.selectedEmployeeModify == "") ? 'null' : self.selectedEmployeeModify.UserName,
                    Sfdcaccountid: (self.selectedAccount == undefined || self.selectedAccount == "") ? 'null' : self.selectedAccount.AccountID,
                    Soldtoid: (self.selectedCustomer == undefined || self.selectedCustomer == "") ? 'null' : self.selectedCustomer.CustomerID,
                    Contractid: (self.contractNumberTxt == undefined || self.contractNumberTxt == "") ? 'null' : self.contractNumberTxt
                };
            }

            myQuoteData.getQuotes(searchParams)
                       .$promise
                       .then(function (results) {
                           if (results != null) {
                               for (var i = 0 ; i < results.length;i++)
                               {
                                   results[i] = results[i].Header;
                               }
                               
                               self.addMyQuotesSearchResults(results);
                           }
                           else {
                               notifications.showWarning('Quotes not found');
                           }
                       })
                       .catch(function (response) {
                           notifications.showError('Error searching quotes');
                           console.log(response);
                       }
                   );
        };

        self.addMyQuotesSearchResults = function (results) {
            //console.log(results);
            self.quoteDatasCollection = results;
            notifications.showSuccess(results.length + " " + ((results.length == 1) ? "quote" : "quotes") + " found.");           
            myQuoteData.quoteDatasCollection = self.quoteDatasCollection;
        };

        self.fieldsReset = function () {
            self.quoteNumberTxt = null;
            self.dt = null;
            self.selectedAccount = null;
            self.selectedEmployeeCreate = null;
            self.selectedEmployeeModify = null;
            self.selectedCustomer = null;
            self.contractNumberTxt = null;
        };

        self.formatDate = function (date) {
            var monthNames = [
              "January", "February", "March",
              "April", "May", "June", "July",
              "August", "September", "October",
              "November", "December"
            ];

            var formatDate = new Date(date);
            var day = formatDate.getDate();
            var monthIndex = formatDate.getMonth();
            var year = formatDate.getFullYear();
            
            return day + ' ' + monthNames[monthIndex] + ' ' + year;
        };

        self.init();


    }]);
